-- CocStatusline   CocUi
vim.cmd [[
function! CocError() abort
  let info = get(b:, 'coc_diagnostic_info', {})
  if empty(info) | return '' | endif
  let msgs = []
  if get(info, 'error', 0)
    call add(msgs, '' . info['error'])
  endif
  return join(msgs)
endfunction

function! StatusError() abort
  let Error = CocError()
  if Error != 0
    return Error
  else
    return "0"
  endif
endfunction

function! CocWarning() abort
  let info = get(b:, 'coc_diagnostic_info', {})
  if empty(info) | return '' | endif
  let msgs = []
  if get(info, 'warning', 0)
    call add(msgs, '' . info['error'])
  endif
  return join(msgs)
endfunction

function! StatusWarning() abort
  let Warning = CocWarning()
  if Warning != 0
    return Warning
  else
    return "0"
  endif
endfunction


function! Git_add() abort
    return Git_Diff(0)
endfunction
function! Git_modified() abort
    return Git_Diff(1)
endfunction
function! Git_remove() abort
    return Git_Diff(2)
endfunction
let g:Diff_icon = ['  ','  ','  ']
function Git_adds() abort
    if  Git_add() != '0'
        let add = Git_Diff(0)
    else
      if g:StatusIcon == 1
        let add = g:Diff_icon[0]."0"
      else
        let add = ''
      endif
    endif
    return add
endfunction
function! Git_modifieds() abort
    if  Git_Diff(1) != '0'
        let modified = Git_Diff(1)
    else
      if g:StatusIcon == 1
        let modified = g:Diff_icon[1]."0"
      else
        let modified = ''
      endif
    endif
    return modified
endfunction
function! Git_removes() abort
    if Git_Diff(2) != '0'
        let remove= Git_Diff(2)
    else
      if g:StatusIcon == 1
        let remove = g:Diff_icon[2]."0"
      else
        let remove = ''
      endif
    endif
    return remove
endfunction

function! Git_Diff(type) abort
    let l:diff_data = split(get(b:, 'coc_git_status'))
    let l:difficon = g:Diff_icon[a:type]
    let l:diff_flags = ['+','\~','-'][a:type]
    for item in l:diff_data
        if matchend(item,l:diff_flags) > 0
            return substitute(item, l:diff_flags, l:difficon, '')
        endif
    endfor
endfunction


function! StatusGitBranch() abort
  if get(b:, 'gitbranch_pwd', '') !=# expand('%:p:h') || !has_key(b:, 'gitbranch_path')
    call GitbranchDetect(expand('%:p:h'))
  endif
  if has_key(b:, 'gitbranch_path') && filereadable(b:gitbranch_path)
    let branch = get(readfile(b:gitbranch_path), 0, '')
    if branch =~# '^ref: '
      return ' '. substitute(branch, '^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '').' '
    elseif branch =~# '^\x\{20\}'
      return ' '. branch[:6]
    endif
  endif
  return ''
endfunction


function! s:gitbranch_dir(path) abort
  let path = a:path
  let prev = ''
  while path !=# prev
    let dir = path . '/.git'
    let type = getftype(dir)
    if type ==# 'dir' && isdirectory(dir.'/objects') && isdirectory(dir.'/refs') && getfsize(dir.'/HEAD') > 10
      return dir
    elseif type ==# 'file'
      let reldir = get(readfile(dir), 0, '')
      if reldir =~# '^gitdir: '
        return simplify(path . '/' . reldir[8:])
      endif
    endif
    let prev = path
    let path = fnamemodify(path, ':h')
  endwhile
  return ''
endfunction

function! GitbranchDetect(path) abort
  unlet! b:gitbranch_path
  let b:gitbranch_pwd = expand('%:p:h')
  let dir = s:gitbranch_dir(a:path)
  if dir !=# ''
    let path = dir . '/HEAD'
    if filereadable(path)
      let b:gitbranch_path = path
    endif
  endif
endfunction
]]



-- 
---- 状态栏信息
local StatusFileName
local StatusError
local StatusWarning
local StatusDiagnostic
local StatuslineLeft
local StatusCoc
local StatusGitBranch
local ErrorIcon            = " "
local WarningIcon          = " "
local StatusDiffadd        = "%#StatuslineCocFun#%{Git_adds()}%* "
local StatusDiffmodified   = "%#StatuslineCocWarning#%{Git_modifieds()}%* "
local StatusDiffremove     = "%#StatuslineCocError#%{Git_removes()}%* "
local StatusCursorPosition = "Ln %l,Col %c "
local StatusCocGit         = StatusDiffadd .. StatusDiffmodified .. StatusDiffremove
local fielType             = " %{&filetype} "
local fileSpaces           = " Spaces:%{shiftwidth()} "
local fileCoding           = " %{''.(&fenc!=''?&fenc:&enc).''} "
StatusFileName             = " %t %="
StatusGitBranch            = "%#CocStatusGitColor#%{StatusGitBranch()}%*"
StatusError                = "%#StatuslineCocError#" .. ErrorIcon .. "%{StatusError()} %*"
StatusWarning              = "%#StatuslineCocWarning#" .. WarningIcon .. "%{StatusWarning()} %*"
StatusCoc                  = StatusCocGit
StatusDiagnostic           = StatusError .. StatusWarning
if vim.g.colors_name == "vscode" then
  vim.cmd [[highlight statuslineLeftIcon guibg=#16825D]]
  StatuslineLeft        = "%#statuslineLeftIcon#  %* " .. StatusGitBranch .. StatusDiagnostic .. StatusFileName
  local StatuslineRight = StatusCursorPosition .. fileSpaces .. fileCoding .. fielType .. "  "
  vim.o.statusline      = StatuslineLeft .. StatusCoc .. StatuslineRight
else
  StatuslineLeft = " " .. StatusGitBranch .. StatusDiagnostic .. StatusFileName
  local StatuslineRight = fileCoding .. fielType
  vim.o.statusline = "%#@constructor#█%*" .. StatuslineLeft .. StatusCoc .. StatuslineRight .. "%#@constructor#█%*"
end

-- vim.o.statusline = StatuslineLeft
vim.o.laststatus = 3
-- vim.o.cmdheight = 0
